#!/bin/bash

CMAPI_KEY="${CMAPI_KEY:-somekey123}"
MAX_USER="${MAX_USER:-maxscale}"
MAX_PASS="${MAX_PASS:-C0lumnStore!}"
REP_USER="${REP_USER:-idbrep}"
REP_PASS="${REP_PASS:-C0lumnStore!}"

HOSTS=( "$@" )
if [[ "$#" -eq 0 ]]; then
    HOSTS=(mx1 mx2)
fi

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'

count=1
for h in "${HOSTS[@]}"
do
    export MAXAPI$count="curl --write-out '%{http_code}' --silent --output /dev/null -X POST -u admin:mariadb $h:8989/v1"
    (( count++ ))
done

PAYLOAD1='{"data":{"id":"pm3","type":"servers","attributes":{"parameters":{"address":"'$PM3'","protocol":"MariaDBBackend"}}}}'
PAYLOAD2='{"data":{"id":"pm2","type":"servers","attributes":{"parameters":{"address":"'$PM2'","protocol":"MariaDBBackend"}}}}'
PAYLOAD3='{"data":{"id":"pm1","type":"servers","attributes":{"parameters":{"address":"'$PM1'","protocol":"MariaDBBackend"}}}}'
PAYLOAD4='{"data":{"id":"mcs_service","type":"services","attributes":{"router":"readwritesplit","parameters":{"user":"'$MAX_USER'","password":"'$MAX_PASS'","master_accept_reads":true}},"relationships":{"servers":{"data":[{"id":"pm1","type":"servers"},{"id":"pm2","type":"servers"},{"id":"pm3","type":"servers"}]}}}}'
PAYLOAD5='{"data":{"id":"mcs_listener","type":"listeners","attributes":{"parameters":{"protocol":"MariaDBClient","port":3306}},"relationships":{"services":{"data":[{"id":"mcs_service","type":"services"}]}}}}'
PAYLOAD6='{"data":{"id":"mcs_monitor","type":"monitors","attributes":{"module":"mariadbmon","parameters":{"user":"'$MAX_USER'","password":"'$MAX_PASS'","replication_user":"'$REP_USER'","replication_password":"'$REP_PASS'","enforce_read_only_slaves":true,"auto_failover":true,"auto_rejoin":true,"cooperative_monitoring_locks":"majority_of_all"}},"relationships":{"servers":{"data":[{"id":"pm1","type":"servers"},{"id":"pm2","type":"servers"},{"id":"pm3","type":"servers"}]}}}}'

RESOURCE1='servers'
RESOURCE2='services'
RESOURCE3='listeners'
RESOURCE4='monitors'

SERVERS1=("$MAXAPI1/$RESOURCE1 -d '$PAYLOAD1'" "$MAXAPI1/$RESOURCE1 -d '$PAYLOAD2'" "$MAXAPI1/$RESOURCE1 -d '$PAYLOAD3'")
SERVICE1="$MAXAPI1/$RESOURCE2 -d '$PAYLOAD4'"
LISTENER1="$MAXAPI1/$RESOURCE3 -d '$PAYLOAD5'"
MONITOR1="$MAXAPI1/$RESOURCE4 -d '$PAYLOAD6'"

SERVERS2=("$MAXAPI2/$RESOURCE1 -d '$PAYLOAD1'" "$MAXAPI2/$RESOURCE1 -d '$PAYLOAD2'" "$MAXAPI2/$RESOURCE1 -d '$PAYLOAD3'")
SERVICE2="$MAXAPI2/$RESOURCE2 -d '$PAYLOAD4'"
LISTENER2="$MAXAPI2/$RESOURCE3 -d '$PAYLOAD5'"
MONITOR2="$MAXAPI2/$RESOURCE4 -d '$PAYLOAD6'"

# Add Servers To MaxScale 1
count=1
for i in "${SERVERS1[@]}"
do  
    RESULT1=$(bash -c "$i")
    if [[ "$RESULT1" == 403 ]]; then
        printf "Adding PM$count to MaxScale 1 ... ${YELLOW}already exists${NC}\n"
    else
        if [[ "$RESULT1" == 204 ]]; then
            printf "Adding PM$count to MaxScale 1 ... ${GREEN}done${NC}\n"
        else
            printf "Adding PM$count to MaxScale 1 ... ${RED}fail${NC}\n"
            exit 1
        fi
    fi
    (( count++ ))
done

# Add Servers To MaxScale 2
count=1
for i in "${SERVERS2[@]}"
do
    RESULT1=$(bash -c "$i")
    if [[ "$RESULT1" == 403 ]]; then
        printf "Adding PM$count to MaxScale 2 ... ${YELLOW}already exists${NC}\n"
    else
        if [[ "$RESULT1" == 204 ]]; then
            printf "Adding PM$count to MaxScale 2 ... ${GREEN}done${NC}\n"
        else
            printf "Adding PM$count to MaxScale 2 ... ${RED}fail${NC}\n"
            exit 1
        fi
    fi
    (( count++ ))
done

# Add Router To MaxScale 1
RESULT2=$(bash -c "$SERVICE1")
if [[ "$RESULT2" == 403 ]]; then
    printf "Adding SERVICE to MaxScale 1 ... ${YELLOW}already exists${NC}\n"
else
    if [[ "$RESULT2" == 204 ]]; then
        printf "Adding SERVICE to MaxScale 1 ... ${GREEN}done${NC}\n"
    else
        printf "Adding SERVICE to MaxScale 1 ... ${RED}fail${NC}\n"
        exit 1
    fi
fi

# Add Router To MaxScale 2
RESULT2=$(bash -c "$SERVICE2")
if [[ "$RESULT2" == 403 ]]; then
    printf "Adding SERVICE to MaxScale 2 ... ${YELLOW}already exists${NC}\n"
else
    if [[ "$RESULT2" == 204 ]]; then
        printf "Adding SERVICE to MaxScale 2 ... ${GREEN}done${NC}\n"
    else
        printf "Adding SERVICE to MaxScale 2 ... ${RED}fail${NC}\n"
        exit 1
    fi
fi

# Add Listener To MaxScale 1
RESULT3=$(bash -c "$LISTENER1")
if [[ "$RESULT3" == 403 ]]; then
    printf "Adding LISTENER to MaxScale 1 ... ${YELLOW}already exists${NC}\n"
else
    if [[ "$RESULT3" == 204 ]]; then
        printf "Adding LISTENER to MaxScale 1 ... ${GREEN}done${NC}\n"
    else
        printf "Adding LISTENER to MaxScale 1 ... ${RED}fail${NC}\n"
        exit 1
    fi
fi

# Add Listener To MaxScale 2
RESULT3=$(bash -c "$LISTENER2")
if [[ "$RESULT3" == 403 ]]; then
    printf "Adding LISTENER to MaxScale 2 ... ${YELLOW}already exists${NC}\n"
else
    if [[ "$RESULT3" == 204 ]]; then
        printf "Adding LISTENER to MaxScale 2 ... ${GREEN}done${NC}\n"
    else
        printf "Adding LISTENER to MaxScale 2 ... ${RED}fail${NC}\n"
        exit 1
    fi
fi

# Add Monitor To MaxScale 1
RESULT4=$(bash -c "$MONITOR1")
if [[ "$RESULT4" == 403 ]]; then
    printf "Adding MONITOR to MaxScale 1 ... ${YELLOW}already exists${NC}\n"
else
    if [[ "$RESULT4" == 204 ]]; then
        printf "Adding MONITOR to MaxScale 1 ... ${GREEN}done${NC}\n"
    else
        printf "Adding MONITOR to MaxScale 1 ... ${RED}fail${NC}\n"
        exit 1
    fi
fi

# Add Monitor To MaxScale 2
RESULT4=$(bash -c "$MONITOR2")
if [[ "$RESULT4" == 403 ]]; then
    printf "Adding MONITOR to MaxScale 2 ... ${YELLOW}already exists${NC}\n"
else
    if [[ "$RESULT4" == 204 ]]; then
        printf "Adding MONITOR to MaxScale 2 ... ${GREEN}done${NC}\n"
    else
        printf "Adding MONITOR to MaxScale 2 ... ${RED}fail${NC}\n"
        exit 1
    fi
fi